<?php
namespace backend\modules\ding\controllers;

use backend\controllers\BaseController;
use common\models\base\SearchModel;
use common\models\ding\Check;
use common\models\ding\Name;
use common\traits\Curd;
use Yii;

class CheckController extends BaseController
{
    use Curd;
    /**
     * @var \yii\db\ActiveRecord
     */
    public $modelClass = Check::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $start_time = Yii::$app->request->post('start_time', date('Y-m-d',strtotime('-1 year')));
        $end_time = Yii::$app->request->post('end_time', date('Y-m-d',strtotime('+1 month')));
        error_reporting(0);
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'relations' => ['name' => ['name']], // 关联 member 表的 nickname 字段
            'partialMatchAttributes' => ['name.name'], // 模糊查询，注意 member_nickname 为关联表的别名 表名_字段
            'defaultOrder' => [
                'id' => SORT_ASC,//排序
            ],
            'pageSize' => $this->pageSize,
        ]);
        $res = $this->actionCommon();
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query->andFilterWhere(['between', 'userCheckTime', strtotime($start_time), strtotime($end_time)])->orderBy('userCheckTime desc');
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'res' => $res,
            'start_time' => $start_time,
            'end_time' =>$end_time,
            'searchModel' => $searchModel,
        ]);
    }
    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        error_reporting(0);
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        //$model->sender_id = Yii::$app->user->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' => $model,
        ]);
    }
    /**
     * 表连接方法
     */
    public function actionCommon(){
        $data = Name::find()->asArray()->all();
        $res = [];
        foreach ($data as  $v){
            $res[$v['id']] = $v['name'];
        }
        return $res;
    }
}